<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <!--
       首先要进行双向绑定数据
       通过双向绑定获取数据向接口发送请求
       将返回的数据重新进行渲染
      -->
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option v-for="item in statusList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="searchInfo">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addCard')">添加月卡</el-button>
      <el-button type="primary" @click="batchDelCar">批量删除</el-button>
      <div class="Proportion">
        <i class="el-icon-warning-outline" />
        本园区共计有{{ spaceNumber }} 个车位，月卡用户{{ cardCount }} 人，车位占有率{{ proportion }}
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="状态" :formatter="formatStatus" prop="cardStatus" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="obj">
            <el-button size="mini" type="text" @click="renewCar(obj.row.id)">续费</el-button>
            <el-button size="mini" type="text" @click="showCardDetail(obj.row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="editCarDetail(obj.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delCar(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :current-page="params.page"
        :page-size="params.pageSize"
        layout="total, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCartList, DelCarAPI, ProportionAPI } from '@/api/card'

export default {
  data() {
    return {
      params: {
        // 页数
        page: 1,
        // 条数
        pageSize: 2,
        // 车牌号码
        carNumber: '',
        // 车主姓名
        personName: '',
        // 状态
        cardStatus: null
      },
      // 车行列表
      list: [],
      // 等待删除列表,
      delList: [],
      // 条数
      total: 0,
      statusList: [
        { id: 0, name: '有效' },
        { id: 1, name: '无效' },
        { id: null, name: '全部' }
      ],
      // 车位
      spaceNumber: 0,
      // 占有率
      proportion: 0,
      // 月卡数量
      cardCount: 0
    }
  },
  created() {
    // 首先先获取到列表
    this.getList()
    this.getProportion()
  },
  methods: {
    async getList() {
      // 对车行列表接口发起请求
      const res = await getCartList(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    formatStatus(row) {
      // row.cardStatus
      // 使用映射的方式进行读取数据并返回
      const mapping = {
        0: '有效',
        1: '无效'
      }
      return mapping[row.cardStatus]
    },
    // 切换页面
    handleCurrentChange(page) {
      // page参数就是你所点击的当前页
      this.params.page = page
      // 当页面进行改变的时候，那么就重新获取数据
      this.getList()
    },
    // 对用户信息进行搜索(搜索功能)
    searchInfo() {
      // 从第一页开始进行搜索
      this.params.page = 1
      this.getList()
    },
    // 携带id进行传参(编辑功能)
    editCarDetail(id) {
      this.$router.push({
        path: '/addCard',
        query: {
          id
        }
      })
    },
    // 删除车辆信息
    delCar(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(id)
        await DelCarAPI(id)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async handleSelectionChange(value) {
      this.delList = value
    },
    // 批量删除
    async batchDelCar() {
      await DelCarAPI(this.delList.map(item => item.id))
      this.getList()
      this.$message.success('批量删除成功')
    },
    // 查看月卡详情
    showCardDetail(id) {
      // 携带id进行跳转
      this.$router.push({
        path: '/viewMonthCard',
        query: {
          id

        }
      })
    },
    // 续费月卡
    renewCar(id) {
      this.$router.push({
        path: '/renewMonthCard',
        query: {
          id,
          check: true
        }
      })
    },
    // 车位占有率
    async getProportion() {
      const res = await ProportionAPI()
      console.log(res)
      this.spaceNumber = res.data.spaceNumber
      this.proportion = res.data.proportion
      this.cardCount = res.data.cardCount
    }
  }

}
</script>

<style lang="scss" scoped>
.Proportion{
  float: right;
  height: 30px;
  width: 400px;
  box-sizing: border-box;
  border: 1px solid #91d5ff;
  background-color: #e6f7ff;
  text-align: center;
  line-height: 30px;
  margin-top: 5px;
  font-size: 12px;
}
.Proportion > .el-icon-warning-outline{
  color: #1890ff;

  font-size: 14px;

}
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}

</style>
